package com.peanut.baby.mvp.main;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.User;
import com.tencent.mars.xlog.Log;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/28.
 */

public class MainActivityPresenter implements MainActivityContract.Presenter {
    private static final String TAG = "MainActivityPresenter";
    private MainActivityContract.View view;
    private Activity activity;

    public MainActivityPresenter(MainActivityContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void updateUStatus(int status) {
//        Map<String, String> params = new HashMap<>();
//
//        params.put("userToken", InitManager.getInstance().getUser().userToken);
//        params.put("userStatus", status + "");
//        Log.d(TAG,"userToken: "+InitManager.getInstance().getUser().userToken);
        Observable observable = RetrofitClient.getInstance().updateStatus(status,InitManager.getInstance().getUser().userId+"",InitManager.getInstance().getUser().userToken);
        observable.compose(BaseScheduler.<BaseResponse<User>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
                    @Override
                    protected void onHandleSuccess(User o) {
                        InitManager.getInstance().getUser().userStatus = o.userStatus;
                        InitManager.getInstance().updateLocalUser();
                        Log.d("AAA", "profile updated");
                        view.onUpdateUStatusResult(true, "");
                    }

                    @Override
                    protected void onHandleError(String code, String msg) {
                        view.onUpdateUStatusResult(false, msg);
                    }
                });
    }
}
